package tech.popsoft.cip.manage.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tech.popsoft.cip.manage.service.ApiAppService;
import tech.popsoft.cip.manage.service.ApiMessagePermissionService;
import tech.popsoft.cip.manage.service.ApiMessageTopicService;
import tech.popsoft.platform.common.base.BaseServiceImpl;
import tech.popsoft.cip.manage.entity.ApiApp;
import tech.popsoft.cip.manage.entity.ApiMessagePermission;
import tech.popsoft.cip.manage.entity.ApiMessageTopic;
import tech.popsoft.cip.manage.mapper.ApiMessagePermissionMapper;

/**
 * 消息权限 服务实现类
 * @author wqliu
 * @date 2021-08-21
 *
 */
@Service
public class ApiMessagePermissionServiceImpl extends BaseServiceImpl<ApiMessagePermissionMapper, ApiMessagePermission> implements ApiMessagePermissionService {

    @Autowired
    private ApiAppService apiAppService;

    @Autowired
    private ApiMessageTopicService apiMessageTopicService;


    @Override
    public ApiMessagePermission init() {
        ApiMessagePermission entity=new ApiMessagePermission();

        return entity;
    }

    @Override
    public boolean checkPermission(String appCode, String topicCode) {
        ApiApp app = apiAppService.getByCode(appCode);
        ApiMessageTopic messageTopic = apiMessageTopicService.getByCode(topicCode);
        Integer count = this.lambdaQuery().eq(ApiMessagePermission::getAppId, app.getId())
                .eq(ApiMessagePermission::getMessageTopicId, messageTopic.getId())
                .count();
        return count>0;

    }
}
